package _18_剑指OfferII;

public class _073_剑指OfferII狒狒吃香蕉 {

    public static void main(String[] args) {

        _073_剑指OfferII狒狒吃香蕉 v = new _073_剑指OfferII狒狒吃香蕉();

        int[] ins = {3, 6, 7, 11};
        System.out.println(v.minEatingSpeed(ins, 8));

    }

    public int minEatingSpeed(int[] piles, int h) {
        int begin = 1;
        int end = 1000000000;
        // 找到小于等于某个数c
        int result = -1; // 找到某个区间  都用这种方法
        while (begin <= end) {
            int mid = begin + ((end - begin) >> 1);
            int hours = eatHours(piles, mid);
            if (hours > h) { // 时间尽量大，并且小于h
                begin = mid + 1;
            } else { // 尽量往小的方向靠
                result = mid;
                end = mid - 1;
            }
        }
        return result;
    }

    // 最好别用Math.ceil， 使用(pile - 1) / cap + 1，替代效率块不少
    public int eatHours(int[] piles, int cap) {
        int total = 0;
        for (int pile : piles) {
            total += (pile - 1) / cap + 1;
        }
        return total;
    }

}
